package stark.app.function.imgmodel;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.stark.imgmodel.lib.core.ImgModelBean;
import com.stark.imgmodel.lib.core.ImgModelUtil;
import com.stark.imgmodel.lib.ui.ImgModelMakeActivity;

import stark.app.R;
import stark.app.databinding.FragmentImgModelListBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.constant.Extra;
import stark.common.bean.StkPicTemplateDetail;

/**
 * Author：daizhongbin on 2025/5/21
 * Email：1027154338@qq.com
 * Desc:
 */
public class ImgModelListFragment extends BaseNoModelFragment<FragmentImgModelListBinding> {

    private ImgModelTestAdapter mTestAdapter;
    private ImgModelNoTestAdapter mNoTestAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_img_model_list;
    }

    @Override
    protected void initView() {
        Bundle bundle = getArguments();
        boolean isTest = true;
        if (bundle != null && bundle.containsKey(Extra.MODE)) {
            isTest = bundle.getBoolean(Extra.MODE);
        }
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        mDataBinding.rvData.setLayoutManager(layoutManager);
        if (isTest) {
            ImgModelTestAdapter testAdapter = new ImgModelTestAdapter();
            mTestAdapter = testAdapter;
            testAdapter.setOnItemClickListener(this);
            mDataBinding.rvData.setAdapter(testAdapter);
        } else {
            ImgModelNoTestAdapter noTestAdapter = new ImgModelNoTestAdapter();
            mNoTestAdapter = noTestAdapter;
            noTestAdapter.setOnItemClickListener(this);
            mDataBinding.rvData.setAdapter(noTestAdapter);
        }

        mDataBinding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (mTestAdapter != null) {
                    mTestAdapter.reqFirstPageData((success, stkPicTemplates) -> {
                        mDataBinding.smartRefreshLayout.finishRefresh(success);
                    });
                } else {
                    mNoTestAdapter.reqFirstPageData((success, stkPicTemplates) -> {
                        mDataBinding.smartRefreshLayout.finishRefresh(success);
                    });
                }
            }
        });
        mDataBinding.smartRefreshLayout.autoRefresh();
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        int id = 0;
        if (adapter instanceof ImgModelTestAdapter) {
            id = ((ImgModelTestAdapter) adapter).getItem(position).id;
        } else {
            id = ((ImgModelNoTestAdapter) adapter).getItem(position).getId();
        }
        showDialog(getString(com.stark.common.res.R.string.loading));
        StkResApi.getPicTemplateDetailById(this, id, new IReqRetCallback<StkPicTemplateDetail>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable StkPicTemplateDetail result) {
                dismissDialog();
                if (result != null) {
                    String json = GsonUtils.toJson(result.json_content);
                    ImgModelBean modelBean = ImgModelUtil.loadImgModelFromJson(json);
                    ImgModelMakeActivity.start(getContext(), modelBean, result.name);
                } else {
                    ToastUtils.showShort(msg);
                }
            }
        });
    }
}
